﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SysTrayIcon.cs" company="">
//   
// </copyright>
// <summary>
//   The sys tray icon.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace TinyRadio.UserInterface
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    /// <summary>The sys tray icon.</summary>
    public class SysTrayIcon : IDisposable
    {
        #region Fields

        /// <summary>The _tray icon.</summary>
        private NotifyIcon trayIcon;

        #endregion

        #region Constructors and Destructors

        /// <summary>Initializes a new instance of the <see cref="SysTrayIcon"/> class.</summary>
        /// <param name="text">The text.</param>
        /// <param name="icon">The icon.</param>
        /// <exception cref="ArgumentNullException">text or icon are null.</exception>
        public SysTrayIcon(string text, Icon icon)
        {
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentNullException("text");
            }

            if (icon == null)
            {
                throw new ArgumentNullException("icon");
            }

            this.trayIcon = new NotifyIcon
                                 {
                                     Visible = true, 
                                     Text = text, 
                                     Icon = icon, 
                                     ContextMenu = new ContextMenu()
                                 };
        }

        #endregion

        #region Public Events

        /// <summary>The click.</summary>
        public event EventHandler Click
        {
            add
            {
                this.trayIcon.Click += value;
            }

            remove
            {
                this.trayIcon.Click += value;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>The add.</summary>
        /// <param name="caption">The caption.</param>
        /// <param name="onClick">The on click.</param>
        /// <returns>The <see cref="SysTrayIcon"/>.</returns>
        public SysTrayIcon Add(string caption, EventHandler onClick)
        {
            this.trayIcon.ContextMenu.MenuItems.Add(caption, onClick);

            return this;
        }

        /// <summary>The dispose.</summary>
        public void Dispose()
        {
            this.trayIcon.Visible = false;
            this.trayIcon.Dispose();
            this.trayIcon = null;
        }

        #endregion
    }
}